<?php

namespace app\api\controller\feiyu;


use app\common\exception\UploadException;
use think\Config;
use think\Hook;
use baiying\lib\BYTokenClient;
use think\Db;

/**
 * 公共接口
 */
class Xiansuo extends Base {

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize() {
        parent::_initialize();
    }
    
    
    public function huichuan(){
        
        $request_data = $this->request->param();
        $where = null;
        $starttime = strtotime('yesterday');
        $endtime = $starttime + 86400;
        if(!empty($request_data['ids'])){
            $where['a.id'] = $request_data['ids'];
        }else{
//            $where['a.createtime']= ['between',[$starttime,$endtime]];
        }
        
        $list = db('ec_aiuser_to_ecuser_feiyu_log')->alias('a')
            ->field('b.byid,a.clue_convert_state,a.id')
            ->join('ec_aiuser_to_ecuser_feiyu b','a.crmId = b.crmId')      
            ->where($where)
             
            ->where([
                'a.is_huichuan'=>1,
                'a.huichuan_status'=>0
            ])->order('a.id desc')->select();

        foreach ($list as $value){
            $info = db('ec_feiyu_xiansuo')->field('clue_id,clue_source,advertiser_id')->where('id',$value['byid'])->find();
            $item_request_data = [
                'advertiser_ids'=>[$info['advertiser_id']],
                'clue_id'=>$info['clue_id'],
                'source'=>$info['clue_source'],
                'clue_convert_state'=>$value['clue_convert_state'],
            ];
            
            
            $item_reponse_data = $this->clueHuiChuan($item_request_data);
            qqq('huichuan',$item_reponse_data);
            if(empty($item_reponse_data['code']) && $item_reponse_data['message'] == 'OK'){
                db('ec_aiuser_to_ecuser_feiyu_log')->where('id',$value['id'])->update([
                    'huichuan_status'=>1,
                    'huichuantime'=>time()
                ]);
            }
        }
        $this->success('成功');
    }
    
    
    protected function clueHuiChuan($request_data){
         $url = 'https://ad.oceanengine.com/open_api/2/tools/clue/callback/';
        $headers=['Access-Token:'.$this->access_token];     
        
        $response_data = httpCurlpost($url, json_encode($request_data), $headers);
        $result = json_decode($response_data,true);
        return $result;
    }

    



    public function advertiserList(){
        $url = 'https://ad.oceanengine.com/open_api/oauth2/advertiser/get/?access_token=' . $this->access_token;
//        $headers=['Access-Token:'.$this->access_token];     
        
        $response_data = sendGetRequest($url, [], []);
        $result = json_decode($response_data,true);
        var_dump($result);exit;
    }
    
    public function xiansuoList(){
        $start_time = '2024-09-01';
        $end_time = date('Y-m-d');
        $url = 'https://ad.oceanengine.com/open_api/2/tools/clue/get/';
        $data = ['advertiser_ids'=>$this->advertiser_ids,'start_time'=>$start_time,'end_time'=>$end_time];
        $headers=['Access-Token:'.$this->access_token];     
        $url = 'https://ad.oceanengine.com/open_api/2/tools/clue/get/?advertiser_ids=[1663930686224392]&start_time=2024-09-01&end_time=2024-09-07';
 
        $response_data = sendGetRequest($url, $data, $headers);
        var_dump($response_data);exit;
    }




    
    
   
    

}
